<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/commons :: head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客管理</title>
<!--    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">-->
<!--    <link rel="stylesheet" th:href="@{/css/semantic.min.css}">-->
<!--    <link rel="stylesheet" th:href="@{/css/style.css}">-->
</head>
<body>

    <!--头部导航开始-->
    <!--thymeleaf 片段替换-->
    <div th:replace="admin/commons :: header(1)"></div>
    <!--头部导航结束-->

    <!--二级导航-->
    <div class="ui attached pointing menu">
        <div class="ui container">
            <div class="right menu">
                <a th:href="@{/admin/blogs/input}" class="item">发布</a>
                <a th:href="@{/admin/blogs}" class="item active teal">列表</a>
            </div>
        </div>
    </div>

    <!--中间部分开始-->
    <div class="v-container-small v-padded-tb-big">
        <div class="ui container">
            <div class="ui secondary segment form">

                <!--分页条件查询隐藏对象，分页调用post方法-->
                <input type="hidden" name="page">

                <div class="inline fields">
                    <div class="field">
                        <input type="text" name="title" placeholder="标题">
                    </div>

                    <div class="field">
                        <div class="ui labeled action input">
                            <div class="ui type selection dropdown">
                                <input type="hidden" name="typeId">
                                <i class="dropdown icon"></i>
                                <div class="default text">分类</div>
                                <div class="menu">
                                    <div th:each="type : ${types}" class="item" th:text="${type.name}" th:data-value="${type.id}" >错误日志</div>
                                    <!--thymleaf模板引擎下隐藏下面的内容，html下显示-->
                                    <!--/*-->
                                    <div class="item" data-value="2">开发手册</div>
                                    <div class="item" data-value="3">动态</div>
                                    <div class="item" data-value="4">日记</div>
                                    <!--*/-->
                                </div>
                            </div>
                            <button class="ui clear button">Clear</button>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" name="recommend" id="recommend">
                            <label for="recommend">推荐</label>
                        </div>
                    </div>
                    <div class="field">
                        <button type="button" id="search-btn" class="ui mini teal basic button"><i class="search icon"></i>搜索</button>
                    </div>
                </div>
            </div>

            <!--加一个div,使用jquery局部渲染-->
            <div id="table-container">
                <table th:fragment="blogList" class="ui teal celled sortable compact table">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>标题</th>
                        <th>类型</th>
                        <th>推荐</th>
                        <th>状态</th>
                        <th>更新时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="blog,iterStatu : ${page.content}">
                        <td th:text="${iterStatu.count}"></td>
                        <td th:text="${blog.title}"></td>
                        <td th:text="${blog.type.name}"></td>
                        <td th:text="${blog.recommended} ? '是' : '否'"></td>
                        <td th:text="${blog.published} ? '发布' : '草稿'"></td>
                        <td th:text="${blog.updatedTime}"></td>
                        <td>
                            <a th:href="@{/admin/blogs/{id}/input(id=${blog.id})}" class="ui mini teal basic button">编辑</a>
                            <a th:href="@{/admin/blogs/{id}/delete(id=${blog.id})}" class="ui mini red basic button">删除</a>
                        </td>
                    </tr>
                    </tbody>
                    <tfoot>
                    <tr>
                        <th colspan="7">
                            <!--js提交表单传递查询参数实现分页条件查询，要模板引擎自定义属性-->
                            <div class="ui mini pagination menu" th:if="${page.totalPages} >1">
                                <a onclick="page(this)" th:attr="data-page=${page.number} - 1" th:unless="${page.first}" class="icon item">
                                    <i class="left chevron icon"></i>
                                </a>
                                <a href="#" class="item">1</a>
                                <a href="#" class="item">2</a>
                                <a href="#" class="item">3</a>
                                <a onclick="page(this)" th:attr="data-page=${page.number} + 1" th:unless="${page.last}" class="icon item">
                                    <i class="right chevron icon"></i>
                                </a>
                            </div>
                            <a th:href="@{/admin/blogs/input}" class="ui mini right floated blue basic button"><i class="add icon"></i>新增</a>
                        </th>
                    </tr>
                    </tfoot>
                </table>
                <!--添加博客是否成功提示信息-->
                <div class="ui success message" th:unless="${#strings.isEmpty(msg)}">
                    <i class="close icon"></i>
                    <div class="header">提示:</div>
                    <p th:text="${msg}"></p>
                </div>
            </div>
        </div>
    </div>
    <!--中间部分结束-->
    <br>
    <br>

    <!--底部部分开始-->
    <!--thymeleaf 片段替换-->
    <div th:replace="admin/commons :: #footer"></div>
    <!--底部部分结束-->


<!--<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>-->
<!--<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>-->
<!--<script th:src="@{/js/jquery-3.6.0.min.js}"></script>-->
<!--<script th:src="@{/js/semantic.min.js}"></script>-->

<!--/*/<th:block th:replace="admin/commons :: script">/*/-->
    <!--thymeleaf 引入js资源，这三行不为注释-->
<!--/*/</th:block>/*/-->
<script>
    $('.menu.toggle').click(function () {
        $('.v-item').toggleClass('v-mobile-hidden')
    });

    $('.ui.dropdown').dropdown({
        on: 'hover'
    });

    //消息提示关闭
    $('.message .close').on('click', function() {
        $(this).closest('.message').transition('fade');
    });

    /*分页条件查询*/
    function page(obj) {
        //根据属性选择器给隐藏域表单赋值,将点击的obj对象转为jquery对象，使用data获取自定义的属性的值
        $("[name='page']").val($(obj).data('page'));
        //使用ajax来局部刷新表格，页面刷新会将表单的查询条件变空
        loadData();
    }

    $('#search-btn').click(function () {
        $("[name='page']").val(0);
        loadData();
    })

    function loadData() {
        //ajax,发送请求，使用thymeleaf模板引擎来渲染请求地址
        $("#table-container").load(/*[[@{/admin/blogs/search}]]*/"/admin/blogs/search",{
            title: $("[name='title']").val(),
            typeId: $("[name='typeId']").val(),
            recommend: $("[name='recommend']").prop("checked"),
            page: $("[name='page']").val(),
        })
    }

    //清除按钮
    $('.ui.clear.button')
        .on('click', function() {
            $('.ui.type.dropdown')
                .dropdown('clear')
            ;
        });
</script>
</body>
</html>

